'use strict';

// todoPage controller
app.controller('todoPage', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    //console.log($('#aaa').html("111111"))
    $scope.openAddUrl = function(lx){
        $state.go($scope.allUrl[lx],{docId:"new"});
    };
    $scope.getInfo=function(index){
        $("#searchInput").val("");
        $scope.indexSearch = index;
        getTodoList(index,"");
    };

    $scope.searchTodo = function(){
        var searchInput = $("#searchInput").val();
        getTodoList($scope.indexSearch,searchInput);
    }
    $scope.indexSearch = 0;
    $scope.allUrl = {
        salesorder:'app.saleOrder',
        salesreturn:'app.saleReturn',
        salesout:'app.saleWarehousing',
        purchase:'app.purchasingOrder',
        purchasein:'app.purchasingWarehousing',
        purchasereturn:'app.purchasingReturn',
        productionplan:'app.productionPlan',
        processprocess:'app.processingSheet',
        othergoodin:'app.otherWarehousing',
        othergoodout:'app.otherOutlets',
        transfer:'app.allocation',
        inventory:'app.goodsInventory',
        commodityassembly:'app.commodityAssembly',
        merchandisedisassembly:'app.merchandiseDisassembly'
    }
    $scope.gotoDoc=function(item){
        $state.go($scope.allUrl[item.personalWorkDbUrl],{docId:item.personalWorkDocId});
    };
    $scope.steps={percent:20, step1:true, step2:false, step3:false, step4:false};
    $scope.isshow=false;
//    模仿请求得到的数据
    $scope.columns = [
        {
            title:'文件标题',
            show:true
        },
        {
            title:'业务类型',
            show:true
        },
        {
            title:'发送时间',
            show:true
        },
        {
            title:'发送人',
            show:true
        },
        {
            title:'制单时间',
            show:true
        },
        {
            title:'文件状态',
            show:true
        },
        {
            title:'当前处理人',
            show:true
        },
        {
            title:'制单人',
            show:true
        }
    ];
    $scope.saveDate=[];
    $scope.saveDate1=[];
    $scope.saveDate2=[];
    $scope.saveDate3=[];
    function makeData(data,d){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd HH:mm:ss");
            item.updateTime = $filter("date")(item.updateTime, "yyyy/MM/dd HH:mm:ss");
            if(item.personalWorkFlowName==="已办结"){
                item.status1 = 'label bg-danger';
            }else{
                item.status1 = 'label bg-info';
            }
        })

        $scope.saveDate=[];
        $scope.saveDate1=[];
        $scope.saveDate2=[];
        $scope.saveDate3=[];
        switch (d) {
            case  0: $scope.saveDate = data;
                break;
            case  1: $scope.saveDate1 = data;
                break;
            case  2: $scope.saveDate2 = data;
                break;
            case  3: $scope.saveDate3 = data;
                break;
        }
        setTimeout(function(){
            $('.table').trigger('footable_redraw');
        }, 100);


    }
    $scope.showNumberData = {};
    function getTodoList(index,searchInput){
        var content = {
            "startTime":null,
            "endTime":null,
            "type":null,
            "content":searchInput,
        };
        $scope.isshow=true;
        toaster.postInfo('/a_yongan/list/personalworktodo/'+index,content).then(function(data){
            if(data && data.data && data.data.data){
                if(data.data.data.length===0){
                    $scope.isshow=true;
                }else{
                    $scope.isshow=false;
                }
                makeData(data.data.data,index);
                toaster.getInfo('/a_yongan/list/todonumber',null).then(function(data){
                    if(data && data.data && data.data.data){
                        $scope.showNumberData = data.data.data;
                        setTimeout(showNumber(),100);
                    }
                });
            }
        });
    }
    getTodoList(0,"");
    function showNumber(){
        var ids = ['todo','hasdo','mystart','nosubmit'];
        var show = function(id){
          if($("#"+id+" a"))  {
              if($("#"+id+" a .badge")[0]){
              }else{
                  var num = $scope.showNumberData[id] || 0;
                  $("#"+id+" a").append('<span class="badge badge-sm up bg-danger pull-right-xs">'+num+'</span>');
              }
          }else{
              show(id);
          }
        };
        ids.forEach(function(item){
            show(item);
        });
    }

}])
;